$(function(){
    getSysSetInfo();

    $('.nav-tabs a[data-toggle="tab"]').on('show.bs.tab', function (e) {
        var tag = $(e.target).attr('aria-controls');
        switch (tag) {
            case 'contactRedeemCode':  // 联系人发码
                selectParameterSetting();
                break;
        }
    });

    $('#redeemCodeSwitch').on('change', function () {
        var status = $(this).prop('checked');
        updateContactcodeStatus(Number(!status));
    })
})

function selectParameterSetting() {
    $.ajax({
        url: '/contact/selectParameterSetting',
        type: 'POST',
        dataType: 'json',
        success: function(data) {
            if (data.code == '000000') {
                // 0发码 1不发码
                var status = data.data.contactCodeStatus;
                if (status) {
                    // 不发码
                    $('#redeemCodeSwitch').prop('checked', false);
                } else {
                    // 发码
                    $('#redeemCodeSwitch').prop('checked', true);
                }
            } else {
                $.alert('修改失败!');
            }
        },
        error: function () {
            console.log('error')
        }
    })
}

function updateContactcodeStatus(contactCodeStatus) {
    $.ajax({
        url: '/contact/updateContactcodeStatus',
        type: 'POST',
        dataType: 'json',
        data: {
            contactCodeStatus
        },
        success: function(data) {
            if (data.code == '000000') {
                if (contactCodeStatus) {
                    $.alert('关闭成功');
                } else {
                    $.alert('开启成功');
                }
            } else {
                $.alert('修改失败!');
            }
        },
        error: function () {
            console.log('error')
        }
    })
}

function getSysSetInfo() {
    $.ajax({
        type: "get",
        url: "/systemParameterSetting/selectSystemParameterSetting",
        async: false,
        success: function (data) {
            if(data.code == '000000' && data.data) {
                $("#sysParameterId").val(data.data.sysParameterId);
                $("#noFollowOutDate").val(data.data.noFollowOutDate);
                $("#outReceiveDate").val(data.data.outReceiveDate);
                $("#noFollowPlanDate").val(data.data.noFollowPlanDate);
                $("#maxCustomerNum").val(data.data.maxCustomerNum);
                $("#maxCustomerProtectNum").val(data.data.maxCustomerProtectNum);
                $("#dateReceiveCustomerMaxNum").val(data.data.dateReceiveCustomerMaxNum);
                $("#newReceiveNoFollowOutDate").val(data.data.newReceiveNoFollowOutDate);
                $("#memberExpirationDate").val(data.data.memberExpirationDate);
                $("#internalTrainingRelease").val(data.data.internalTrainingRelease);
            }
        }
    });
}

//提交表单
function subSysSet() {
    var sysParameterId = $("#sysParameterId").val();
    var noFollowOutDate = $("#noFollowOutDate").val();//无跟进记录最长时间踢出设置
    var outReceiveDate = $("#outReceiveDate").val();//踢出后领取间隔时间设置
    var noFollowPlanDate = $("#noFollowPlanDate").val();//无跟进记录提醒保护时间设置
    var maxCustomerNum = $("#maxCustomerNum").val();//普通最大客户数设置
    var maxCustomerProtectNum = $("#maxCustomerProtectNum").val();//最大客户保护数设置
    var dateReceiveCustomerMaxNum = $("#dateReceiveCustomerMaxNum").val();//当天领取客户数量最大设置
    var newReceiveNoFollowOutDate = $("#newReceiveNoFollowOutDate").val();//新领取客户无跟进记录时间踢出设置
    var memberExpirationDate = $("#memberExpirationDate").val();//会员过期时间期限(月)
    var internalTrainingRelease = $("#internalTrainingRelease").val();//内训释放时间限制(天)
    var data = {
        "sysParameterId": sysParameterId,
        "noFollowOutDate": noFollowOutDate,
        "outReceiveDate": outReceiveDate,
        "noFollowPlanDate": noFollowPlanDate,
        "maxCustomerNum": maxCustomerNum,
        "maxCustomerProtectNum": maxCustomerProtectNum,
        "dateReceiveCustomerMaxNum": dateReceiveCustomerMaxNum,
        "newReceiveNoFollowOutDate": newReceiveNoFollowOutDate,
        "memberExpirationDate" : memberExpirationDate,
        "internalTrainingRelease" : internalTrainingRelease
    }
    $.ajax({
        url: '/systemParameterSetting/systemParameterSetting',
        async: false,
        type: "post",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(data),
        success: function (data) {
            if (data.code == '000000') {
                $.alert('修改成功!');
            } else {
                $.alert('修改失败!');
            }
        },
        error: function () {
            console.log('error')
        }
    });

}

